package cn.com.css.taxInspection.gt3.service.impl;

import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.gt3.entity.JcJjajEntity;
import cn.com.css.taxInspection.gt3.mapper.JcJjajMapper;
import cn.com.css.taxInspection.gt3.service.*;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class JcJjajServiceImpl extends ServiceImpl<JcJjajMapper, JcJjajEntity> implements JcJjajService {

    @Resource
    private JcJjajService jcJjajService;

    @Resource
    private HttpSessionService httpSessionService;

    @Override
    public IPage<JcJjajEntity> queryJjajxxList(JcJjajEntity jcJjajEntity) throws ParseException {
        String swjgDm = httpSessionService.getLogSwjgDm();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        IPage<JcJjajEntity> page = new Page<>(jcJjajEntity.getPage(), jcJjajEntity.getLimit());
        LambdaQueryWrapper<JcJjajEntity> queryWrapper = Wrappers.lambdaQuery();

        //检查部门
        if (TsBqUtils.isNotNull(jcJjajEntity.getJcbmDm())) {
            queryWrapper.eq(JcJjajEntity::getJcbmDm, jcJjajEntity.getJcbmDm());
        } else if (TsBqUtils.isNull(jcJjajEntity.getJcbmDm()) && TsBqUtils.isNotNull(jcJjajEntity.getAjszjgDm())) {
            if (!Constants.ALL.equals(jcJjajEntity.getAjszjgDm())) {
                queryWrapper.eq(JcJjajEntity::getAjszjgDm, jcJjajEntity.getAjszjgDm());
            }
        } else {
            queryWrapper.eq(JcJjajEntity::getAjszjgDm, swjgDm);
        }

        // 案件编号
        if (TsBqUtils.isNotNull(jcJjajEntity.getJcajbh())) {
            queryWrapper.like(JcJjajEntity::getJcajbh, jcJjajEntity.getJcajbh());
        }
        // 案源编号
        if (TsBqUtils.isNotNull(jcJjajEntity.getJcaybh())) {
            queryWrapper.like(JcJjajEntity::getJcaybh, jcJjajEntity.getJcaybh());
        }
        // 纳税人名称
        if (!StringUtils.isEmpty(jcJjajEntity.getNsrmc())) {
            queryWrapper.like(JcJjajEntity::getNsrmc, jcJjajEntity.getNsrmc());
        }
        // 纳税人识别号
        if (TsBqUtils.isNotNull(jcJjajEntity.getNsrsbh())) {
            queryWrapper.like(JcJjajEntity::getNsrsbh, jcJjajEntity.getNsrsbh());
        }
        // 案件状态
        if (TsBqUtils.isNotNull(jcJjajEntity.getAjjcztDm())) {
            String[] list = jcJjajEntity.getAjjcztDm().split(","); // 按逗号分割并转换成 List
            for (String str : list) {
                queryWrapper.or().likeRight(JcJjajEntity::getAjjcztDm, str);
            }
        }
        //立案时间
        if (!StringUtils.isEmpty(jcJjajEntity.getLastartTime())) {
            queryWrapper.ge(JcJjajEntity::getLarq, sdf.parse(jcJjajEntity.getLastartTime()));
            queryWrapper.le(JcJjajEntity::getLarq, sdf.parse(jcJjajEntity.getLaendTime()));
        }

        queryWrapper.orderByDesc(JcJjajEntity::getLarq);
        return this.page(page, queryWrapper);
    }

    @Override
    public List<JcJjajEntity> exportJjajList(JcJjajEntity jcJjajEntity) throws ParseException {
        String swjgDm = httpSessionService.getLogSwjgDm();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        LambdaQueryWrapper<JcJjajEntity> queryWrapper = Wrappers.lambdaQuery();
        //检查部门
        if (TsBqUtils.isNotNull(jcJjajEntity.getJcbmDm())) {
            queryWrapper.eq(JcJjajEntity::getJcbmDm, jcJjajEntity.getJcbmDm());
        } else if (TsBqUtils.isNull(jcJjajEntity.getJcbmDm()) && TsBqUtils.isNotNull(jcJjajEntity.getAjszjgDm())) {
            if (!Constants.ALL.equals(jcJjajEntity.getAjszjgDm())) {
                queryWrapper.eq(JcJjajEntity::getAjszjgDm, jcJjajEntity.getAjszjgDm());
            }
        } else {
            queryWrapper.eq(JcJjajEntity::getAjszjgDm, swjgDm);
        }
        // 案件编号
        if (TsBqUtils.isNotNull(jcJjajEntity.getJcajbh())) {
            queryWrapper.like(JcJjajEntity::getJcajbh, jcJjajEntity.getJcajbh());
        }
        // 案源编号
        if (TsBqUtils.isNotNull(jcJjajEntity.getJcaybh())) {
            queryWrapper.like(JcJjajEntity::getJcaybh, jcJjajEntity.getJcaybh());
        }
        // 纳税人名称
        if (!StringUtils.isEmpty(jcJjajEntity.getNsrmc())) {
            queryWrapper.like(JcJjajEntity::getNsrmc, jcJjajEntity.getNsrmc());
        }
        // 纳税人识别号
        if (TsBqUtils.isNotNull(jcJjajEntity.getNsrsbh())) {
            queryWrapper.like(JcJjajEntity::getNsrsbh, jcJjajEntity.getNsrsbh());
        }
        // 案件状态
        if (TsBqUtils.isNotNull(jcJjajEntity.getAjjcztDm())) {
            String[] list = jcJjajEntity.getAjjcztDm().split(","); // 按逗号分割并转换成 List
            for (String str : list) {
                queryWrapper.or().likeRight(JcJjajEntity::getAjjcztDm, str);
            }
        }
        //立案时间
        if (!StringUtils.isEmpty(jcJjajEntity.getLastartTime())) {
            queryWrapper.ge(JcJjajEntity::getLarq, sdf.parse(jcJjajEntity.getLastartTime()));
            queryWrapper.le(JcJjajEntity::getLarq, sdf.parse(jcJjajEntity.getLaendTime()));
        }
        queryWrapper.orderByDesc(JcJjajEntity::getLarq);
        return jcJjajService.list(queryWrapper);
    }

}
